package org.jcut.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.validation.Valid;

import org.jcut.bean.Comment;

import org.jcut.data.Data;
import org.jcut.service.impl.CommentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/comment")
public class CommnetController {
	@Autowired
	private CommentServiceImpl service;
	@Autowired
	private Data data;

	/**
	 * @author 胡琪
	 * @param String fId 订单id
	 * 
	 */
	@RequestMapping("/isComment")
	public Object isComment(String fId) {
		
		long i = service.isComment(fId);
		if (i > 0) {
			data.setData(i);
			data.setStatus("1");
			data.setMsg("已评论");
		} else {
			data.setData(i);
			data.setStatus("0");
			data.setMsg("未评论");
		}
		return data;
	}

	/**
	 * @author 胡琪
	 * @param String uId 用户id
	 * @param String cId 商品id
	 * @param String comment 评论内容
	 * @param String uName 用户名
	 * @param String fId 订单id
	 * @param String cStar 评星
	 */
	@RequestMapping("/add")
	public Object add(@Valid Comment comment, Errors e) {

		List<ObjectError> error = e.getAllErrors();
		if (error.size() == 0) {
			comment.setReplyId("1");
			comment.setId(UUID.randomUUID().toString());
			comment.setDis(1);
			comment.setTime(new Date());
			int i = service.insert(comment);
			if (i > 0) {
				data.setData(i);
				data.setStatus("1");
				data.setMsg("评论成功");
			} else {
				data.setData(i);
				data.setStatus("0");
				data.setMsg("服务器异常");
			}
		} else {
			for (ObjectError ee : error) {
				data.setData(ee.getDefaultMessage());
				data.setMsg(ee.getDefaultMessage());
			}
			data.setStatus("0");
			
		}

		return data;
	}
	@RequestMapping("/selectByCid")
	public Object selectByCid(@RequestParam("cId")String cId,@RequestParam(value="pageNum",required = false)String pageNum,@RequestParam(value="pageSize",required = false) String pageSize) {
		if ((pageNum != null && !("".equals(pageNum))) && (pageSize != null && !("".equals(pageSize)))) {
			PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		}
		PageInfo<Comment> pageInfo = new PageInfo<>(service.selectByCid(cId));
		
	
		data.setData(pageInfo);
		data.setStatus("1");
		data.setMsg("查询成功");
		return data;
	}
	@RequestMapping("/selectAdmin")
	public Object selectAdmin(@RequestParam(value="pageNum",required = false)String pageNum,@RequestParam(value="pageSize",required = false) String pageSize) {
		if ((pageNum != null && !("".equals(pageNum))) && (pageSize != null && !("".equals(pageSize)))) {
			PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		}
		PageInfo<Comment> pageInfo = new PageInfo<>(service.selectAdmin());
		
		
		data.setData(pageInfo);
		data.setMsg("查询成功");
		data.setStatus("1");
		return data;
	}
	@RequestMapping("/updateDis")
	public Object updateDis(Comment comment) {
		int i=service.updateDis(comment);
		if(i>0) {
			data.setData(i);
			data.setMsg("修改成功");
			data.setStatus("1");
		}else {
			data.setData(i);
			data.setMsg("服务器异常");
			data.setStatus("0");
			
		}
		return data;
	}
}
